import { detailOrder, paySuccess } from '@/api/order.js';
import { aliPayOrder } from '@/api/aliPay.js';

export default {
	data() {
		return {
			orderSn: null,
			payType: 1,//支付方式：0->未支付；1->微信；2->支付宝；3->其他
			orderInfo: {}
		};
	},
	onLoad(options) {
		this.orderSn = options.orderSn;
		this.loadData();
	},
	methods: {
		//生成确认单信息
		async loadData() {
			let params = {
				orderSn: this.orderSn
			}
			detailOrder(params).then(response => {
				this.orderInfo = response.data;
			});
		},
		//选择支付方式
		changePayType(type) {
			this.payType = type;
		},
		//确认支付
		confirm() {
			//支付宝支付
			if(this.payType == 2){
				this.alipay()
			}
			
			let data = {
				orderSn: this.orderSn,
				payType: this.payType
			}
			paySuccess(data).then(response => {
				uni.redirectTo({
					url: '/pages/order/success/success'
				})
			});
		},
		
		//支付宝支付
		alipay(){
			//设置订单号
			let data = {
				orderSn: this.orderSn
			}
			//获取支付信息
			aliPayOrder(data).then(response => {
				var orderInfoStr = response.data;
				
				//设置沙箱环境
				var EnvUtils = plus.android.importClass('com.alipay.sdk.app.EnvUtils');
				EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
				
				//发起支付宝支付，调起支付宝客户端
				uni.requestPayment({
					provider: 'alipay',
					orderInfo: orderInfoStr,
					success: function (res) {
						console.log('success:' + JSON.stringify(res));
					},
					fail: function (err) {
						console.log('fail:' + JSON.stringify(err));
					}
				});
			});
		}
	}
}